var http = require('http');
var fs = require('fs');
var url = require('url');

const app = http.createServer(function(req, res){
    var pathname = url.parse(req.url).pathname;
    console.log('收到来自 ' + pathname + ' 的请求。');
    console.log(pathname.substring(1));
    fs.readFile(pathname.substring(1), function(err, data){
        if(err){
            console.log(err);
            res.writeHead(404, {'Content-Type':'text/html'});
        }else{
            res.writeHead(200, {'Content-Type':'text/html'});
            res.write(data.toString());
        }
        res.end();
    });
});

app.listen(8080, ()=>{console.log('服务器已启动，端口号：8080')});